<?php
class Zendvn_Model_Table_NewsCategory extends Zendvn_Model_Nested{
	protected $_name = "news_category";
	protected $_primary = "id";
	public $_alias = 'c'; 
	
	protected $_arrParam = array();
	
	public function itemInSelect($arrParam = null,$options = null){
		//====================== Nested =======================		
		$db = $this->getAdapter();
		$exclude_id = !empty($arrParam['id'])?$arrParam['id']:0;
		$result = $this->listItem(0, 'all', $exclude_id);
		if($options == null){
			$result[0]['name'] = '-- ROOT --';
		}
		if($options['task'] == 'admin-add'){
			$select 			= $db->select()->from($this->_name)->order('lft ASC');
			$result 			= $db->fetchAll($select);
		}
		if($options['task'] == 'item-add'){
			$result = $this->listItem(0, 'all');
			unset($result[0]);
		}
		return $result;
	}
	
	public function sortItem($arrParam = null,$options = null){
		if($options == null){
			if(!empty($arrParam['cid'])){
				$db = $this->getDefaultAdapter();
				$select = $db->select()
							->from($this->_name)
							->order('lft ASC');
				$result = $db->fetchAll($select);
				$data = array();
				foreach ($result as $v){
					$data[] = $v;
				}
				$orderArr = $arrParam['ordering'];
				$this->orderTree($data, $orderArr);
			}
		}
	}
		
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';	
			
			$children = $this->getChildrenNode($arrParam['id'],array('node'=>true));
			if(!empty($children)){	
				foreach ($children as $val):
					$data = array('status'=>$status);
					$where = 'id = ' . (int)$val['id'];
					$this->update($data, $where);
				endforeach;
			}
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				foreach ($arrParam['cid'] as $val){
					$fields = array('id');
					$children = $this->getChildrenNode($val,array('node'=>true,'fields'=>$fields));
					
					if(!empty($children)){	
						foreach ($children as $val):
							$data = array('status'=>$status);
							$where = 'id = ' . (int)$val['id'];
							$this->update($data, $where);
						endforeach;
					}
					
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->newsCategory($arrParam,$options,$this);
	}
	public function getItem($arrParam = null,$options = null){
		
		if($options == null){
			if(!empty($arrParam['id'])){
				$result = $this->getNodeInfo($arrParam['id']);
			}
		}
		if($options['task'] == 'item'){
			$where = 'id = ' . (int)$arrParam['parents'];
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'info'){
			$db = $this->getAdapter();
			if(!empty($arrParam['id'])){
				$select = $this->_sqlItem;
				$select->joinLeft('users AS u1','u1.id = '.$this->_alias .'.created_by', array ('u1.email AS created_by'))
						->joinLeft('users AS u2','u2.id = '.$this->_alias .'.modified_by', array ('u2.email AS modified_by'))
						->joinLeft($this->_name .' AS c2','c2.id = '.$this->_alias .'.parents', array ('c2.name AS cname'))
						->where($this->_alias .".id = ?",$arrParam['id'],'INTEGER');
				$result = $db->fetchRow($select);
			}	
		}
		return $result;
	}
	public function totalItem($arrParam = null,$options = null){
		$result = $this->totalNode() - 1;
		return $result;
	}
	public function listAll($arrParam = null,$options = null){
		if($options == null){
			
			$select = $this->_sqlItem;
			$select->joinLeft('users AS u1','u1.id = '.$this->_alias .'.created_by', array ('u1.email AS created_by'));
			$select->joinLeft('users AS u2','u2.id = '.$this->_alias .'.modified_by', array ('u2.email AS modified_by'));
			$arrFilter = $arrParam['arrFilter'];
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);
			}
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where($this->_alias . '.status = ?',$arrFilter['status']);
			}
			if($arrFilter['level']>0){
				$select->where($this->_alias . '.level <= ?',$arrFilter['level'],1);
			}
			if($arrFilter['parentNode']>1){
				$nodeInfo = $this->getNodeInfo($arrFilter['parentNode']);
				$select->where('lft >= ?',$nodeInfo['lft'],1);
				$select->where('rgt <= ?',$nodeInfo['rgt'],1);
			}
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
			}
			if($arrParam['action'] == 'edit'){
				$select->where('id != ' . $arrParam['id']);	
			}
				
		}
		$select = $this->_sqlItem;
		$result = $this->listItem(0, 'all');
		$this->orderGroup($result);
		return $result;
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		$this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null,$options){
		//==================== Add Action ==================
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		$data 		= array(
							'name'			=> $arrParam['name'],		
							'router'		=> $arrParam['router'],			
							'image'			=> (!empty($arrParam['image']))?$arrParam['image']:$arrParam['current_image'],
							'parents'		=> $arrParam['parents'],
							'description'	=> $arrParam['description'],	
							'html_descript'	=> $arrParam['html_descript'],	
							'html_keyword'	=> $arrParam['html_keyword'],						
							'status'		=> ($arrParam['status']=='') ? 'inactive':$arrParam['status'],
							'created'		=> date('Y-m-d H:i:s'),
							'created_by'	=> $idUser,
							'params'	=> Zend_Json::encode($arrParam['cparam']),
							);
			
		$this->insertNode($data, $arrParam['parents'], $options);
	}
	
	public function adminEdit($arrParam = null){
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		$data 		= array(
							'name'			=> $arrParam['name'],		
							'router'		=> $arrParam['router'],			
							'image'			=> (!empty($arrParam['image']))?$arrParam['image']:$arrParam['current_image'],
							'parents'		=> $arrParam['parents'],
							'description'		=> $arrParam['description'],	
							'html_descript'	=> $arrParam['html_descript'],	
							'html_keyword'	=> $arrParam['html_keyword'],						
							'status'		=> ($arrParam['status']=='') ? 'inactive':$arrParam['status'],
							'modified'		=> date('Y-m-d H:i:s'),
							'modified_by'	=> $idUser,
							'params'		=> Zend_Json::encode($arrParam['cparam']),
							);
		$this->updateNode($data, $arrParam['id'], $arrParam['parents']);
	}
}